package com.water.note.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.water.note.common.Result;
import com.water.note.service.AchievementService;
import com.water.note.service.TodoService;
import com.water.note.service.WishService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页控制器
 */
@RestController
@RequestMapping("/home")
@RequiredArgsConstructor
public class HomeController {

    private final TodoService todoService;
    private final WishService wishService;
    private final AchievementService achievementService;

    /**
     * 获取首页数据概览
     * 包括：待办事项总数和完成进度、愿望清单总数和实现进度、成果记录总数和本月新增数
     */
    @GetMapping("/overview")
    public Result<Map<String, Object>> getOverview() {
        Long userId = StpUtil.getLoginIdAsLong();
        Map<String, Object> result = new HashMap<>();
        
        // 1. 获取待办事项数据
        Map<String, Object> todoData = todoService.getOverviewData(userId);
        result.put("todo", todoData);
        
        // 2. 获取愿望清单数据
        Map<String, Object> wishData = wishService.getOverviewData(userId);
        result.put("wish", wishData);
        
        // 3. 获取成果记录数据
        Map<String, Object> achievementData = achievementService.getOverviewData(userId);
        result.put("achievement", achievementData);
        
        return Result.success(result);
    }
    
    /**
     * 获取首页列表数据
     * 包括：最近待办事项列表、最近愿望清单列表
     */
    @GetMapping("/list")
    public Result<Map<String, Object>> getHomeList() {
        Long userId = StpUtil.getLoginIdAsLong();
        Map<String, Object> result = new HashMap<>();
        
        // 1. 获取待办事项列表（最近5条）
        List<?> todoList = todoService.getRecentTodos(userId, 5);
        result.put("todoList", todoList);
        
        // 2. 获取愿望清单列表（最近3条）
        List<?> wishList = wishService.getRecentWishes(userId, 3);
        result.put("wishList", wishList);
        
        return Result.success(result);
    }
} 